-- The MIT License
-- 
-- Copyright (c) 2008 Matthew Maravillas, Patrick Quinn
-- 
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
-- 
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
-- 
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.

local util = import('/mods/koth/hook/lua/util.lua')

KothOptions = {
    -- Radius of the hill (a wall is 1 unit wide)
    HillSize = 27,

    -- Total time on the hill needed to win the game, in seconds
    TimeOnHillToWin = 300,

    -- Time until the hill is available for control, in seconds
    HillDelay = 300,
    
    -- Which units are able to control the hill
    ControllingUnits = 'LandSea',

    
    
    -- Allow a player to win immediately if all other players are dead
    AllowAnnihilationVictory = true,
}

KothOptionInfo = {
    {
        title = 'Hill size',
        tooltip = 'The size of the hill.',
        key = 'HillSize',
        choices = {
            {
                text = 'Small',
                tooltip = 'A small hill, suitable for small and medium maps.',
                key = 27,
            },
            
            {
                text = 'Medium',
                tooltip = 'A medium hill, suitable for small and medium maps.',
                key = 50,
            },
            
            {
                text = 'Large',
                tooltip = 'A large hill, suitable for medium and large maps.',
                key = 75,
            },
        },
        default = 1,
    },
    
    {
        title = 'Minutes to win',
        tooltip = 'The amount of hill control time needed to win the game.',
        key = 'TimeOnHillToWin',
        choices = {
            {
                text = '2 minutes',
                tooltip = 'A player wins after 2 minutes of total time on the hill.',
                key = 120,
            },
            
            {
                text = '5 minutes',
                tooltip = 'A player wins after 5 minutes of total time on the hill.',
                key = 300,
            },
            
            {
                text = '10 minutes',
                tooltip = 'A player wins after 10 minutes of total time on the hill.',
                key = 600,
            },
            
            {
                text = '20 minutes',
                tooltip = 'A player wins after 20 minutes of total time on the hill.',
                key = 1200,
            },
            
            {
                text = '30 minutes',
                tooltip = 'A player wins after 30 minutes of total time on the hill.',
                key = 1800,
            },
        },
        default = 2,
    },
    
    {
        title = 'Warmup time',
        tooltip = 'The amount of time before the hill can be controlled.',
        key = 'HillDelay',
        choices = {
            {
                text = 'None',
                tooltip = 'The hill can be controlled immediately.',
                key = 0,
            },
            
            {
                text = '5 minutes',
                tooltip = 'The hill can be controlled 5 minutes after the start of the game.',
                key = 300,
            },
            
            {
                text = '10 minutes',
                tooltip = 'The hill can be controlled 10 minutes after the start of the game.',
                key = 600,
            },
        },
        default = 2,
    },
    
    {
        title = 'Controlling units',
        tooltip = 'Which units are able to control the hill.',
        key = 'ControllingUnits',
        choices = {
            {
                text = 'All',
                tooltip = 'Any unit besides walls can control the hill.',
                key = 'All',
            },
            
            {
                text = 'Land',
                tooltip = 'Any land unit besides walls can control the hill.',
                key = 'Land',
            },
            
            {
                text = 'Sea',
                tooltip = 'Any naval unit can control the hill.',
                key = 'Sea',
            },
            
            {
                text = 'Land & Sea',
                tooltip = 'Any land or naval unit besides walls can control the hill.',
                key = 'LandSea',
            },
            
            {
                text = 'Engineer',
                tooltip = 'Only engineers can control the hill.',
                key = 'Engineer',
            },
            
            {
                text = 'ACU',
                tooltip = 'Only ACUs can control the hill.',
                key = 'Commander',
            },
        },
        default = 3,
    },
}

ControllingUnitsFunc = {
    All = function(unit)
        return EntityCategoryContains(categories.ALLUNITS - categories.KOTHFLAG - categories.WALL, unit) 
    end,
    
    Land = function(unit)
        return EntityCategoryContains(categories.LAND - categories.KOTHFLAG - categories.WALL, unit) 
    end,
    
    Sea = function(unit)
        return EntityCategoryContains(categories.NAVAL, unit) 
    end,
    
    LandSea = function(unit)
        return EntityCategoryContains((categories.LAND + categories.NAVAL) - categories.KOTHFLAG - categories.WALL, unit) 
    end,
    
    Engineer = function(unit)
        return EntityCategoryContains(categories.ENGINEER - categories.COMMAND - categories.SUBCOMMANDER, unit) 
    end,
    
    Commander = function(unit)
        return EntityCategoryContains(categories.COMMAND, unit) 
    end,
}

local OptionsController = Class() {
    __init = function(self)
        -- This seems to be getting called twice, but I'm not sure why. Everything also appears to work fine.
        LOG('__init: Creating OptionsController')
        
        util.CreateEvent(self, 'OptionsSet')
    end,
    
    SetKothOptions = function(self, options)
        LOG('SetKothOptions: ' .. repr(options))
        
        KothOptions = options
        self:OptionsSet()
    end,
    
    GetKothOptionsSetter = function(self)
        return function(options)
            self:SetKothOptions(options)
        end
    end,
}

optionsController = OptionsController()
